<?php
namespace Home\Controller;

use Think\Controller;

class GoodsController extends Controller
{

    //需求详情
    public function need_detail()
    {

        $id = $_GET['id'];
        session('need_url', __ACTION__ . "/id/" . $id);

        $goods = D('Goods');
        $user = D('User');

        $info = $goods->find($id);
        $info['browse_num'] = $info['browse_num'] + 1;
        $goods->save($info);

        //获得商品用户信息
        $user_id = $info['user_id'];
        $userInfo = $user->find($user_id);

        //把用户相关信息添加info中，处理电话和价钱
        $info['user_name'] = $userInfo['user_name'] == '' ? '匿名用户' : $userInfo['user_name'];
        $info['tel'] = empty($info['tel']) ? "无" : $info['tel'];
        $info['price'] = empty($info['price']) ? "待商议" : $info['price'];


        $this->assign('info', $info);
        $this->display();
    }

    //需求详情底层
    public function need_detail_only()
    {

        $id = $_GET['id'];
        session('need_url', __ACTION__ . "/id/" . $id);

        $goods = D('Goods');
        $user = D('User');

        $info = $goods->find($id);
        $info['browse_num'] = $info['browse_num'] + 1;
        $goods->save($info);

        //获得商品用户信息
        $user_id = $info['user_id'];
        $userInfo = $user->find($user_id);

        //把用户相关信息添加info中，处理电话和价钱
        $info['user_name'] = $userInfo['user_name'];
        $info['tel'] = empty($info['tel']) ? "无" : $info['tel'];
        $info['price'] = empty($info['price']) ? "待商议" : $info['price'];


        $this->assign('info', $info);
        $this->display();
    }

    //服务详情
    public function serve_detail()
    {
        
        $id = $_GET['id'];

        $goods = D('Goods');
        $user = D('User');

        $info = $goods->find($id);
        $info['browse_num'] = $info['browse_num'] + 1;
        $goods->save($info);

        //获得商品用户信息
        $user_id = $info['user_id'];
        $userInfo = $user->find($user_id);

        //把用户相关信息添加info中,处理电话和价钱
        $info['user_name'] = $userInfo['user_name'];
        $info['tel'] = empty($info['tel']) ? "无" : $info['tel'];
        $info['price'] = empty($info['price']) ? "待商议" : $info['price'];
        if (session('user_id')) {
            $info['sess_id'] = session('user_id');
        }else{
            $info['sess_id'] = null;
            session('need_url',__SELF__);
            //__CONTROLLER__.'/user_publish_list/user_id/'.$user_id
        }

        $this->assign('info', $info);
        $this->display();

    }

    //
    public function save_needurl(){
        session('need_url',__CONTROLLER__.'/user_publish_list/user_id/'.$_POST['user_id']);
        if (session('need_url')) {
            echo session('need_url');
        }else{
            echo 0;
        }
       
    }

    //我的详情
    public function my_detail()
    {

        $id = $_GET['id'];

        $goods = D('Goods');
        $user = D('User');

        $info = $goods->find($id);

        //获得商品用户信息
        $user_id = session('user_id');
        $userInfo = $user->find($user_id);

        //把用户相关信息添加info中,处理电话和价钱
        $info['user_name'] = $userInfo['user_name'];
        $info['tel'] = empty($info['tel']) ? "无" : $info['tel'];
        $info['price'] = empty($info['price']) ? "待商议" : $info['price'];


        $this->assign('info', $info);

        if ($info['user_judge'] == "need") {
            $this->display('my_need_detail');
        } else {
            $this->display('my_serve_detail');
        }
    }


    //服务底层详情
    public function serve_detail_only()
    {
        //获得服务信息id
        $id = $_GET['id'];

        $goods = D('Goods');
        $user = D('User');

        $info = $goods->find($id);
        $info['browse_num'] = $info['browse_num'] + 1;
        $goods->save($info);

        $user_id = $info['user_id'];
        $userInfo = $user->find($user_id);

        $info['user_name'] = $userInfo['user_name'];
        //处理价钱和电话
        $info['price'] = empty($info['price']) ? "待商议" : $info['price'];
        $info['tel'] = empty($info['tel']) ? "无" : $info['tel'];


        $this->assign('info', $info);
        $this->display();
    }

    //用户其它发布
    public function user_publish_list()
    {

        //获得当前用户id
        $user_id = $_GET['user_id'];

        $goods = D('Goods');

        $arr['user_id'] = $user_id;
        $info = $goods->where($arr)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }
        $this->assign('info', $info);
        $this->display();
    }

    //我的发布
    public function my_publish()
    {

        //获得当前用户id
        $user_id = session('user_id');

        $goods = D('Goods');

        $arr['user_id'] = $user_id;
        $info = $goods->where($arr)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }

        $this->assign('info', $info);
        $this->display();
    }

    //我的发布编辑
    public function my_publish_change()
    {

        //获得当前用户id
        $user_id = session('user_id');

        $goods = D('Goods');

        $arr['user_id'] = $user_id;
        $info = $goods->where($arr)->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }

        $this->assign('info', $info);
        $this->display();
    }

    //我的发布搜索
    public function msg_search()
    {

        //获得当前用户id
        if ($_GET['search_option'] == 1) { //信息
            $where = [
                'goods_title' => ['like', '%' . $_GET['search'] . '%']
            ];
        } else { //用户
            $user_id = M('user')->where(['user_name' => $_GET['search']])->getField('id');
            $where = ['user_id' => $user_id];
        }
        $info = D('Goods')->where($where)->order('id desc')->select();
        foreach ($info as &$val){
            $val['category_name'] = $val['user_judge'] == 'need' ? '需求' : '服务';
        }


        $this->assign('search_option',$_GET['search_option']==1?'信息':'用户');
        $this->assign('search',$_GET['search']);
        $this->assign('info', $info);
        $this->display();
    }

    public function deletePublish()
    {

        $goods_id = $_POST['goods_id'];

        $goods = D('Goods');

        $re = $goods->delete($goods_id);
        if ($re) {
            $msg = D('Message');
            $arr['need_msg_id'] = $goods_id;

            // $sql = "delete from message where need_id = ".$goods_id;
            // $info = $msg -> query($sql);
            $info = $msg->where($arr)->select();
            $ids = '';
            foreach ($info as $k => $v) {
                $ids .= $v['id'] . ",";
            }
            $ids = rtrim($ids, ",");
            $result = $msg->delete($ids);

            echo "1";
        } else {
            echo "0";
        }


    }

    public function getData()
    {

        $sel = $_POST['sel'];
        $sel1 = $_POST['sel1'];
        $user_id = session('user_id');

        $goods = D('Goods');

        $arr['user_id'] = $user_id;

        if ($sel = '10') {//热门
            if ($sel1 == "all") {
                $info = $goods->where($arr)->order('browse_num desc')->limit(20)->select();
            } else {
                $arr['user_judge'] = $sel1;
                $info = $goods->where($arr)->order('browse_num desc')->limit(20)->select();
            }
        } else {//正常类别

            $arr['goods_sort'] = $sel;

            if ($sel1 != "all") {
                $arr['user_judge'] = $sel1;
            }

            $info = $goods->where($arr)->order('browse_num desc')->limit(20)->select();

        }

        echo json_encode($info);
    }


    public function addPublish()
    {

        $user_id = session('user_id');
        $goods = D('Goods');

        $_POST['user_id'] = $user_id;
        $_POST['time'] = date('ymdhis');


        if ($_POST['id'] == 0) {
            $re = $goods->add($_POST);
        } else {
            $re = $goods->save($_POST);
        }

        if ($re) {
            echo session('need_url');
        } else {
            echo "0";
        }


    }


    //需求发布
    public function need_publish()
    {

        $user_id = session('user_id');
        session('need_url', __MODULE__ . "/Goods/need_publish");
        if (isset($user_id)) {

            if ($_GET['goods_id']) {
                session('need_url', __CONTROLLER__ . "/my_detail/id/" . $_GET['goods_id']);
                $goods = D('Goods');
                $info = $goods->find($_GET['goods_id']);
                $this->assign('info', $info);
            }
            $this->display();
        } else {

            $this->redirect('User/login');
        }
    }

    //服务发布
    public function serve_publish()
    {
        session('need_url', __MODULE__ . "/Goods/serve_publish");
        $user_id = session('user_id');

        if (isset($user_id)) {

            if ($_GET['goods_id']) {
                session('need_url', __CONTROLLER__ . "/my_detail/id/" . $_GET['goods_id']);
                $goods = D('Goods');
                $info = $goods->find($_GET['goods_id']);
                $this->assign('info', $info);
            }
            $name = D('User') ->field('user_name')->find($user_id);
            $this->assign('name',$name['user_name']);
            $this->display();
        } else {

            $this->redirect('User/login');
        }

    }

    //我的发布修改
    public function publish_up(){
        session('need_url', __CONTROLLER__ . "/my_detail/id/" . $_GET['goods_id']);
        $info = D('Goods') -> find($_GET['goods_id']);
        $this -> assign('info',$info);
        if ($info['user_judge']=='need') {
            $this -> display('need_publish_up');
        }else{
            $this -> display('serve_publish_up');
        }
    }

}